﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CardClass
{
    public class CardControlAction : Control
    {
        bool bDrag = false;
        Point pCurrent;
        int zIndex = 0;

        Rectangle border;
        SolidColorBrush brush_border = new SolidColorBrush();
        TextBlock function;
        Image logo;
        FrameworkElement implementationRoot;

        RotateTransform rotate;
        TranslateTransform translate;

        public CardControlAction()
        {
            System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("CardClass.CardControlAction.xaml");
            implementationRoot = this.InitializeFromXaml(new System.IO.StreamReader(s).ReadToEnd());

            border = implementationRoot.FindName("border") as Rectangle;
            function = implementationRoot.FindName("function") as TextBlock;
            logo = implementationRoot.FindName("logo") as Image;

            rotate = implementationRoot.FindName("Rotate") as RotateTransform;
            translate = implementationRoot.FindName("Translate") as TranslateTransform;
            // Default layout
            this.Width = 80;
            this.Height = 100;

            rotate.CenterX = this.Width / 2;
            rotate.CenterY = this.Height / 2;

            this.MouseEnter += new MouseEventHandler(CardControl_MouseEnter);
            this.MouseLeave += new EventHandler(CardControl_MouseLeave);
            this.Cursor = Cursors.Hand;
            this.MouseLeftButtonDown += new MouseEventHandler(CardControl_MouseLeftButtonDown);
            this.MouseLeftButtonUp += new MouseEventHandler(CardControl_MouseLeftButtonUp);
            this.MouseMove += new MouseEventHandler(CardControl_MouseMove);
        }

        void CardControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (bDrag)
            {
                Point p = e.GetPosition(null);
                Left = p.X - 200 - pCurrent.X;
                Top = p.Y - 400 - pCurrent.Y;
                //Debug.WriteLine("PL:" + p.X.ToString() + " CL:" + pCurrent.X.ToString() + " L:" + Left.ToString());
            }
        }

        void CardControl_MouseLeftButtonUp(object sender, MouseEventArgs e)
        {
            bDrag = false;
        }

        void CardControl_MouseLeftButtonDown(object sender, MouseEventArgs e)
        {
            ((System.Windows.Media.Visual)sender).CaptureMouse();
            pCurrent.X = e.GetPosition(null).X - Left - 200;
            pCurrent.Y = e.GetPosition(null).Y - Top - 400;
            bDrag = true;
        }

        void CardControl_MouseLeave(object sender, EventArgs e)
        {
            BorderColor = Color.FromRgb(0, 0, 0);
            this.SetValue(Canvas.ZIndexProperty, zIndex);
        }

        void CardControl_MouseEnter(object sender, MouseEventArgs e)
        {
            BorderColor = Color.FromRgb(0, 255, 0);
            zIndex = (int)this.GetValue(ZIndexProperty);
            this.SetValue(Canvas.ZIndexProperty, 500);
        }

        public virtual new double Height
        {
            get { return implementationRoot.Height; }
            set
            {
                implementationRoot.Height = value;
                border.Height = value;
            }
        }

        public virtual new double Width
        {
            get { return implementationRoot.Width; }
            set
            {
                implementationRoot.Width = value;
                border.Width = value;
            }
        }

        public String funcText
        {
            get { return function.Text; }
            set { function.Text = value; }
        }

        public Color BorderColor
        {
            get { return brush_border.Color; }
            set
            {
                brush_border.Color = value;
                border.Stroke = brush_border;
            }
        }

        public void SetURI(DependencyObject downloader, string path)
        {
            logo.SetSource(downloader, path);
        }

        public double Left
        {
            get { return translate.X; }
            set { translate.X = value; }
        }

        public double Top
        {
            get { return translate.Y; }
            set { translate.Y = value; }
        }

        public double Angle
        {
            get { return rotate.Angle; }
            set { rotate.Angle = value; }
        }

    }
}
